package com.bms.admin.bussiness.city.web;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bms.admin.base.index.model.NowUser;
import com.bms.admin.config.annotation.LoginUser;
import com.bms.auth.annotation.RequiresPermissions;
import com.bms.common.base.common.bean.Result;
import com.bms.common.base.web.BaseController;
import com.bms.common.bussiness.city.entity.City;
import com.bms.common.bussiness.city.service.ICityService;
import com.feilong.core.Validator;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 赛事表 前端控制器
 * </p>
 *
 * @author xj
 * @since 2018-08-15
 */
@RestController
@RequestMapping("city")
@Api(value="City-Api",tags="城市接口")
public class CityController extends BaseController{

	@Autowired
	private ICityService cityService;
	
	@GetMapping("checkName")
	@ApiOperation("校验是否存在城市")
	public Result CheckName(@RequestParam String name){
		EntityWrapper<City> ew = new EntityWrapper<>();
		ew.eq("city_name", name);
		int count = cityService.selectCount(ew);
		return json(count);
	}

	@ApiOperation("获取城市列表")
	@GetMapping("list")
	@RequiresPermissions("city:list")
    public Result list(@RequestParam(defaultValue = "1",required=false)Integer pageNum,
    				  @RequestParam(defaultValue = "20",required=false)Integer pageSize,
    				  String cityName) {
		EntityWrapper<City> ew = new EntityWrapper<City>();
		ew.like(Validator.isNotNullOrEmpty(cityName), "city_name", cityName);
		ew.orderBy("create_time",false);
		ew.orderBy("sorts",false);
		return json(cityService.selectPage(new Page<City>(pageNum,pageSize),ew));
    }

	/**
	 * 城市搜索
	 * @param name
	 * @return
	 */
	@PostMapping("search")
    public Result search (@RequestParam String name) {
		EntityWrapper<City> wrapper = new EntityWrapper();
		wrapper.like("city_name", name);

		return json(cityService.selectList(wrapper));
	}

	@ApiOperation("新增城市")
	@PostMapping("save")
	@RequiresPermissions("city:save")
	public Result save(@ApiIgnore @LoginUser NowUser nowUser, @RequestBody City city){
		Date now = new Date();
		city.setCreateTime(now);
		city.setCreateUser(nowUser.getUsername());
		city.setUpdateTime(now);
		cityService.insert(city);
		return success();
	}

	@ApiOperation("获取城市信息")
	@GetMapping("info/{cityId}")
	@RequiresPermissions("city:info")
    public Result select(@PathVariable Long cityId){
		return json(cityService.selectById(cityId));
    }

	@ApiOperation("修改城市")
	@PostMapping("update")
	@RequiresPermissions("city:update")
	public Result update(@ApiIgnore @LoginUser NowUser nowUser,@RequestBody City city){
		city.setUpdateTime(new Date());
		city.setUpdateUser(nowUser.getUsername());
		cityService.updateById(city);
		return success();
	}

	@ApiOperation("删除城市")
	@DeleteMapping("delete")
	@RequiresPermissions("city:delete")
    public Result delete(@RequestBody List<Long> cityIds) {
		cityService.deleteBatchIds(cityIds);
		return success();
    }

	@ApiOperation("设置热门城市")
	@PostMapping("hot/{cityId}")
	@RequiresPermissions("city:hot")
    public Result hot(@ApiIgnore @LoginUser NowUser nowUser,@PathVariable Long cityId) {
		City city = cityService.selectById(cityId);
		if(Validator.isNotNullOrEmpty(city) && city.getHot()!=1){
			city.setHot(1);
			city.setUpdateTime(new Date());
			city.setUpdateUser(nowUser.getUsername());
			cityService.updateById(city);
		}
		return success();
    }

	@ApiOperation("取消热门城市")
	@PostMapping("unhot/{cityId}")
	@RequiresPermissions("city:hot")
    public Result unhot(@ApiIgnore @LoginUser NowUser nowUser,@PathVariable Long cityId) {
		City city = cityService.selectById(cityId);
		if(Validator.isNotNullOrEmpty(city) && city.getHot()!=2){
			city.setHot(2);
			city.setUpdateTime(new Date());
			city.setUpdateUser(nowUser.getUsername());
			cityService.updateById(city);
		}
		return success();
    }
}

